import org.junit.Test;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.IntSupplier;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * git地址

 https://gitee.com/yujianlong1988/java8andDisruptor.git
 * @author yujianlong
 * @create 2018-06-16 11:29
 **/
public class StreamTest1 {




	@Test
	public void stream1(){//值创建
		Stream.of("a","b","c").forEach(System.out::println);
	}

	@Test
	public void stream2(){//数组创建
		Arrays.stream(new int[]{1,2,3}).forEach(System.out::println);
	}

	//文件创建【不讲】

	@Test
	public void stream3(){//iterate无限流
		Stream.iterate(0,n->n+3).limit(10).forEach(System.out::println);
	}

	@Test
	public void stream4(){//generate无限流
		Stream.generate(Math::random).limit(10).forEach(System.out::println);
	}

	@Test
	public void stream5(){//合并流
		Stream<String> sa = Stream.of("a", "b", "c");
		Stream<String> sb = Stream.of("e", "f", "g");
		Stream<String> sc = Stream.of("h", "l", "m");
//		Stream.concat(sa,sb).forEach(System.out::println);//只能俩俩合并
		 Stream.of(sa,sb,sc).flatMap(Function.identity()).forEach(System.out::println);//多个流合并
	}

	public static int fib(int limit){//斐波那契 递归
			if (0==limit){
				return 0;
			}
			if (1==limit){
				return 1;
			}
			return fib(limit-2)+fib(limit-1);
		}

	@Test
	public void stream6(){//斐波那契 递归
		System.out.println(fib(0));
		System.out.println(fib(1));
		System.out.println(fib(2));
		System.out.println(fib(3));
		System.out.println(fib(4));
		System.out.println(fib(5));
	}

	public static int fibFor(int limit){//循环
		if (limit==0) {
			return limit;
		}
		int num0 = 0;
		int num1 = 1;
		int numn = 1;
		for (int i = 0; i <limit-1 ; i++) {
			numn = num0 + num1;
			num0 = num1;
			num1 = numn;
		}
		return numn;
	}

	@Test
	public void stream7(){//斐波那契 循环
		System.out.println(fibFor(0));
		System.out.println(fibFor(1));
		System.out.println(fibFor(2));
		System.out.println(fibFor(3));
		System.out.println(fibFor(4));
		System.out.println(fibFor(5));
	}

	public static int fibIterate(int limit){//java8 Iterate 构建 斐波那契
		//种子
		return  Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]})
				.map(a -> a[0]).skip(limit).findFirst().orElse(0);
	}



	@Test
	public void stream8(){//斐波那契 Iterate
		System.out.println(fibIterate(0));
		System.out.println(fibIterate(1));
		System.out.println(fibIterate(2));
		System.out.println(fibIterate(3));
		System.out.println(fibIterate(4));
		System.out.println(fibIterate(5));
	}
	public static int fibGenerate(int limit){//java8 Generate 构建 斐波那契
		return  Stream.generate(new Supplier<Integer>() {

			int num0 = 0;
			int num1 = 1;
			int count=0;

			@Override
			public Integer get() {

				int numn =0;
				count++;
				if (count==1) {
					return num0;
				} else if (count==2) {
					return num1;
				}else{
					numn = num0 + num1;
					num0 = num1;
					num1 = numn;
					return numn;
				}


			}
		}).skip(limit).findFirst().orElse(0);
	}

	@Test
	public void stream9(){//斐波那契 Generate
		System.out.println(fibGenerate(0));
		System.out.println(fibGenerate(1));
		System.out.println(fibGenerate(2));
		System.out.println(fibGenerate(3));
		System.out.println(fibGenerate(4));
		System.out.println(fibGenerate(5));
	}


	@Test
	public void stream10(){//斐波那契集合 iterate
		List<Integer> collect = Stream.iterate(new int[]{0, 1}, t -> new int[]{t[1], t[0] + t[1]}).map(a -> a[0]).limit(10).collect(Collectors.toList());
		System.out.println(collect);
	}

	@Test
	public void stream11(){//斐波那契集合 gennerate
		IntSupplier fib=new IntSupplier() {
			private int p = 0;
			private int c = 1;
			@Override
			public int getAsInt() {
				int op = p;
				int n = p + c;
				p = c;
				c = n;
				return op;
			}
		};
		List<Integer> collect = IntStream.generate(fib).boxed().limit(10).collect(Collectors.toList());
		System.out.println(collect);
	}


}
